When declaring a
reference variable you must also make it refer to something at the same time.
To declare on you simply make a variable of the type you are going to be
referring to, make up your own name, and put an ampersand (&) in front of it: int &ref; That creates a reference variable called ref of type integer. Of course this doesn't do anything because you can't assign references to other variables at any other time than declaration. So to make ref refer to something we have to do it in the declaration: int &ref = x; This is all assuming we have a variable called x and that it is also an integer (int). But after doing this, anything we do to ref will effect x. |
||
Reference
variables : |
||
•must be declared as referring to something |
||
•must be the same type as the variable they are
referring to (in this case it was int). |
||
•anything you do to a reference variable affects
what it is referring to |